package com.ebeitech.doorapp.domain;

import com.ebeitech.doorapp.db.annotation.Column;
import com.ebeitech.doorapp.db.annotation.Id;
import com.ebeitech.doorapp.db.annotation.PrimaryKey;
import com.ebeitech.doorapp.db.annotation.Table;
import com.ebeitech.library.util.StringUtil;

import org.json.JSONObject;

import java.io.Serializable;

/**
 * Created by alice on 2017/10/24.
 */
@Table(name = "MessageTable")
public class EMessage implements Serializable {

	/**
	 * 系统消息
	 */
	public static final String MESSAGE_TYPE_SYSTEM = "3";
	/**
	 * 公司消息
	 */
	public static final String MESSAGE_TYPE_COMPANY = "2";
	/**
	 * app消息
	 */
	public static final String MESSAGE_TYPE_APP = "1";
	/**
	 * 点对点聊天消息
	 */
	public static final String MESSAGE_TYPE_CHAT = "0";

	/**
	 * 消息状态-未读
	 */
	public static final String READ_FLAG_UNREAD = "0";
	/**
	 * 消息状态-已读
	 */
	public static final String READ_FLAG_READ = "1";

	private int _id;

	private String messageId;
	private String messageType;
	private String title;
	private String content;
	private String time;
	private String typeId;//消息发出特定Id
	private String fromId;//消息发出Id
	private String fromName;//消息发出名字
	private String fromLogo;//消息发出LOGO
	private String invokeUrl;//跳转的URL
	private String readFlag;

	private int count;

	@Id
	@Column(name = "_id")
	public int get_id() {
		return _id;
	}

	public void set_id(int _id) {
		this._id = _id;
	}

	@PrimaryKey
	@Column(name = "messageId")
	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	@Column(name = "messageType")
	public String getMessageType() {
		return messageType;
	}

	public void setMessageType(String messageType) {
		this.messageType = messageType;
	}

	@Column(name = "title")
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "content")
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Column(name = "time")
	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	@Column(name = "typeId")
	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	@Column(name = "fromId")
	public String getFromId() {
		return fromId;
	}

	public void setFromId(String fromId) {
		this.fromId = fromId;
	}

	@Column(name = "fromName")
	public String getFromName() {
		return fromName;
	}

	public void setFromName(String fromName) {
		this.fromName = fromName;
	}


	@Column(name = "fromLogo")
	public String getFromLogo() {
		return fromLogo;
	}

	public void setFromLogo(String fromLogo) {
		this.fromLogo = fromLogo;
	}

	@Column(name = "invokeUrl")
	public String getInvokeUrl() {
		return invokeUrl;
	}

	public void setInvokeUrl(String invokeUrl) {
		this.invokeUrl = invokeUrl;
	}

	@Column(name = "readFlag")
	public String getReadFlag() {
		return readFlag;
	}

	public void setReadFlag(String readFlag) {
		this.readFlag = readFlag;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof EMessage)) {
			return false;
		}
		if (obj == null) {
			return false;
		}
		return StringUtil.getDefaultString(messageId).equals(((EMessage) obj).getMessageId());
	}

	public void initWithJson(String jsonStr) {
		try {
			JSONObject jsonObject = new JSONObject(jsonStr);
			messageId = jsonObject.optString("messageId");
			messageType = jsonObject.optString("messageType");
			time = jsonObject.optString("sendTime");
			title = jsonObject.optString("title");
			content = jsonObject.optString("content");
			invokeUrl = jsonObject.optString("invokeUrl");
			typeId = jsonObject.optString("id");
			String from = jsonObject.optString("from");
			if (MESSAGE_TYPE_COMPANY.equals(messageType)) {
				from = jsonObject.optString("companyInfo");
			} else if (MESSAGE_TYPE_APP.equals(messageType)) {
				from = jsonObject.optString("appInfo");
			}
			JSONObject fromJson = new JSONObject(from);
			if (fromJson != null) {
				fromId = fromJson.optString("id");
				fromName = fromJson.optString("name");
				fromLogo = fromJson.optString("logo");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
